package org.openscheduler.modules.calendar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.openscheduler.modules.scheduler.SchedulerService;
import org.quartz.Calendar;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * TODO : http://jayvilalta.com/blog/2012/08/09/calendars-in-quartz-netpart-1/
 * HolidayCalendar 와  HolidayCalendar 부터 지원하고 차후에 나머지 Calendar 도 지원하도록 하자.
 * @author allbegray
 *
 */

@Service
public class CalendarService {
	
	@Autowired
	SchedulerService schedulerService;
	
	public void addCalendar(String schedName, String calName, Calendar calendar, boolean replace, boolean updateTriggers) throws SchedulerException {
		
//		BaseCalendar 를 상속 받은 6 개의 Calendar 를 수용할 수 있어야 한다.
		
//		HolidayCalendar holidayCalendar = new HolidayCalendar();
//		holidayCalendar.addExcludedDate(new Date());
		
		Scheduler scheduler = schedulerService.getScheduler(schedName);
		scheduler.addCalendar(calName, calendar, replace, updateTriggers);
	}
	
	public Calendar getCalendar(String schedName, String calName) throws SchedulerException {
		Scheduler scheduler = schedulerService.getScheduler(schedName);
		return scheduler.getCalendar(calName);
	}
	
	public List<Calendar> getAllCalendars(String schedName) throws SchedulerException {
		List<Calendar> calendars = new ArrayList<Calendar>();
		Scheduler scheduler = schedulerService.getScheduler(schedName);
		List<String> calendarNames = scheduler.getCalendarNames();
		Collections.sort(calendarNames);
		for (String calName : calendarNames) {
			Calendar calendar = scheduler.getCalendar(calName);
			calendars.add(calendar);
		}
		return calendars;
	}
	
}
